package com.javabasic.algorithm.leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author xiongmin
 * @Description //TODO
 * @Date 2020/1/31 21:53
 * @Version 1.0
 **/
public class GroupAnagrams {

    int N = 26;
    public int book[] = new int[N];


    /**
     * 暴力解法
     *
     * @param strs
     * @return
     */
    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> result = new ArrayList<>();
        HashMap<String, ArrayList<String>> map = new HashMap<>();
        int len = strs.length;
        for (int i = 0; i < len; i++) {
            String key = toSort(strs[i]);
            if (!map.containsKey(key)) {
                map.put(key, new ArrayList<>());
            }
            map.get(key).add(strs[i]);
        }
        System.out.println(map.keySet().size());
        for (String key : map.keySet()) {
            result.add(map.get(key));
        }
        return result;

    }

    private String toSort(String str) {
        int len = str.length();
        for (int i = 0; i < N; i++) {
            book[i] = 0;
        }
        for (int i = 0; i < len; i++) {
            book[str.charAt(i) - 'a']++;
        }
        String result = "";
        for (int i = 0; i < N; i++) {
            if (book[i] != 0) {
                result += i + "-" + book[i] + ",";
            }

        }
        System.out.println(result);
        return result;
    }


}
